home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 01 / startup2.asm < prev    next >
Assembly Source File  |  1990-11-01  |  3KB  |  143 lines

  1.     title    start up code for assembly langauge
  2.     include    asm.inc
  3.  
  4.     dosseg
  5. NULL    segment    para public 'BEGDATA'
  6. NULL    ends
  7.  
  8.     public    psp,startup,dgroup_segment,top_of_stack,initterm,xcbegin,xcend
  9.  
  10.  
  11. XIB    segment word public 'DATA'    ; these segments contain a list of
  12. xibegin    label    word            ;  procedure addresses that the
  13. XIB    ends                ;  start-up code calls before main
  14. XI    segment word public 'DATA'
  15. XI    ends
  16. XIE    segment word public 'DATA'
  17. xiend    label    word
  18. XIE    ends
  19.  
  20. XPB    segment word public 'DATA'    ; these segments contain a list of
  21. xpbegin    label    word            ;  procedure addresses that the
  22. XPB    ends                ;  start-up code calls before
  23. XP    segment word public 'DATA'    ;  returning control to ms-dos.  they
  24. XP    ends                ;  are called only on a full exit.
  25. XPE    segment word public 'DATA'    ;  
  26. xpend    label    word
  27. XPE    ends
  28.  
  29. XCB    segment word public 'DATA'    ; these segments contain a list of
  30. xcbegin    label    word            ;  procedure addresses that the
  31. XCB    ends                ;  start-up code calls before
  32. XC    segment word public 'DATA'    ;  returning control to ms-dos.  they
  33. XC    ends                ;  are ALWAYS called regardless of how
  34. XCE    segment word public 'DATA'    ;  the program terminates.
  35. xcend    label    word
  36. XCE    ends
  37.  
  38. DGROUP    group    XIB,XI,XIE,XPB,XP,XPE,XCB,XC,XCE
  39.  
  40.     .data
  41. psp            dw    0
  42. dgroup_segment        dw    0
  43.  
  44.  
  45.     .data?
  46.     extb    _edata,_end
  47.  
  48. top_of_stack        dw    ?
  49.  
  50.  
  51.     .code
  52.     extn    set_argc_argv,ms_dos,main,exit_program
  53.  
  54.  
  55. ;;    initterm
  56. ;
  57. ;    entry    SI    initializer or terminator list
  58. ;        DI    end of list
  59. ;    may use    AX,BX,CX,DX,DI,SI,DS,ES
  60. ;
  61. initterm proc
  62.     movx    ds,DGROUP_SEGMENT    ; call list of initializers or
  63. iit1:    cmp    si,di            ;  terminators
  64.     jae    iit2            ;   if end of list
  65.     lodsw
  66.     or    ax,ax
  67.     jz    iit1            ;   if NULL address
  68.  
  69.     pushm    di,si,ds
  70.     call    ax
  71.     popm    ds,si,di
  72.     jmp    iit1
  73. iit2:    ret
  74. initterm endp
  75.  
  76.  
  77. ;;    startup
  78. ;
  79. ;    entry    AX    offset stack
  80. ;        ES    program segment prefix
  81. ;        SS    DGROUP
  82. ;    exit    BP    0
  83. ;    may use    AX,BX,CX,DX,DI,SI,DS,ES
  84. ;
  85. startup proc
  86.     cld
  87.     movx    bp,0            ; BP=0 in TIGHT model
  88.  
  89.     mov    ax,ss            ; compensate for Microsoft LINK bug
  90.     mov    dx,DGROUP        ;  where SS is wrong segment
  91.     sub    ax,dx
  92.     jbe    sta1            ;  if SS is DGROUP, must be OPTLINK
  93.     mov    cl,4            ;  else MS-LINK, fix stack pointer
  94.     shl    ax,cl
  95.     mov    ss,dx
  96.     add    sp,ax
  97.  
  98. sta1:    mov    top_of_stack[bp],sp
  99.     mov    psp[bp],es
  100.  
  101.     mov    ax,sp            ; compute number of paragraphs
  102.     add    ax,15            ;  in program
  103.     mov    cl,4
  104.     shr    ax,cl
  105.     mov    bx,ss
  106.     add    bx,ax
  107.     sub    bx,psp[bp]
  108.  
  109.     mov    ah,4Ah            ; shrink program memory block for
  110.     call    ms_dos            ;  dynamic storage system
  111.  
  112.     mov    dgroup_segment[bp],ss
  113.  
  114.     mov    al,0            ; zero BSS segment
  115.     mov    cx,offset DGROUP:_end
  116.     movx    es,DGROUP_SEGMENT
  117.     mov    di,offset DGROUP:_edata
  118.     sub    cx,di
  119.     rep    stosb
  120.  
  121.     call    set_argc_argv        ; process command line
  122.  
  123.     lea    di,xiend        ; call initializers
  124.     lea    si,xibegin
  125.     call    initterm
  126.  
  127.     movx    ds,DGROUP_SEGMENT    ; call user's program
  128.     movx    es,DGROUP_SEGMENT
  129.     call    main
  130.  
  131.     lea    di,xpend        ; call on-exit routines
  132.     lea    si,xpbegin        ;  (must call on-exits before
  133.     call    initterm        ;   terminators!)
  134.  
  135.     lea    di,xcend        ; call terminators
  136.     lea    si,xcbegin
  137.     call    initterm
  138.  
  139.     jmp    exit_program
  140. startup endp
  141.  
  142.     end    startup
  143.